use actix_web::http::StatusCode;
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
mod hello;

async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
    println!("REQ: {:?}", req);
    let r = hello::get_grpc().await;
    match r {
        Ok(response) => Ok(HttpResponse::build(StatusCode::OK)
            .content_type("text/html; charset=utf-8")
            .body(response)),
        Err(_e) => Ok(HttpResponse::Ok().body("req error")),
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "actix_web=info");
    env_logger::init();

    HttpServer::new(|| {
        App::new()
            // enable logger
            .wrap(middleware::Logger::default())
            .service(web::resource("/index.html").to(|| async { "Hello world!" }))
            .service(web::resource("/").to(index))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}
